﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;

namespace PercentageCropper1
{
    public partial class Form1 : Form
    {
        private static int START_X = 0;
        private static int START_Y = 0;

        private String  outputDir;
        private float   heightPerc;
        private long    outputQuality;

        public Form1()
        {
            InitializeComponent();

            this.imagePorcProgressBar.Style = ProgressBarStyle.Continuous;
            this.imagePorcProgressBar.Hide();
            outputQualityTxt.Text = "100";
            HeightPercentage.Text = "7";
        }

        void process(String imageName)
        {
            CropInfo e = new CropInfo(START_X, START_Y, 0, heightPerc);
            ImageCropper c = new ImageCropper(imageName, e);
            Image newImage = c.Action();
            String filenameWithoutPath = Path.GetFileName(imageName);
            string newFileName = outputDir + "\\" + filenameWithoutPath;
            ImageLib.saveJpeg(newFileName, new Bitmap(newImage), outputQuality);
        }

        void updateProgressBar() 
        {
            this.imagePorcProgressBar.Value++;
        }

        public delegate void updateProgressBarCallback();

        private void workerThreadFunc(Object images)
        {
            IEnumerable<String> allImages = (IEnumerable<String>)images;
            foreach (String imageName in allImages)
            {
                process(imageName);
                this.imagePorcProgressBar.Invoke(new updateProgressBarCallback(this.updateProgressBar));
            }
            Invoke(new Action(afterProcess));
        }

        private void afterProcess()
        {
            this.imagePorcProgressBar.Hide();
            this.ProcessBtn.Enabled = true;
            this.addImageBtn.Enabled = true;
        }

        public delegate void afterProcessCallback();
        

        private void button1_Click(object sender, EventArgs e)
        {
            this.ProcessBtn.Enabled = false;
            this.addImageBtn.Enabled = false;
            outputDir   = this.OutputPath.Text;
            heightPerc = (float)(Convert.ToDouble(this.HeightPercentage.Text) / 100);
            outputQuality = Convert.ToInt64(this.outputQualityTxt.Text);
            outputQuality = outputQuality >= 100 ? 100 : outputQuality; 
            IEnumerable<String> allImages =  Directory.EnumerateFiles(@imageRootDir.Text, "*.jpg", SearchOption.AllDirectories);
            int nCount = allImages.Count();
            this.imagePorcProgressBar.Show();
            this.imagePorcProgressBar.Maximum = nCount;
            this.imagePorcProgressBar.Value = 0;

            Thread workThread = new Thread(new ParameterizedThreadStart(workerThreadFunc));
            workThread.Start(allImages);
        }

        private void button2_Click(object sender, EventArgs e) {
            string folderPath = "";
            FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                folderPath = folderBrowserDialog1.SelectedPath;
            }
            imageRootDir.Text = folderPath;
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
